package service;

import model.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * @author ASUS
 */
public class SelectOne {

//    @Test
//    public void test(){
//        System.out.println(select(1));
//    }

    public User select(Integer id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            connection = JDBCConnect.getConnection();
            String sql = "select id,pwd from users where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                Integer idd = resultSet.getInt(1);
                String pwd = resultSet.getString(2);
                user = new User();
                user.setId(idd);
                user.setPwd(pwd);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
}
